 
import QQMap from '../../../utils/qq-map.js'
const app = getApp()

Page({
  data: {
    config: app.globalData.config,
    page: {
      searchCount: false,
      current: 1,
      size: 10
    },
    state:'',
    curIndex: 0,
    loadmore: true,
    wishList: [],
    street:'',
    street_number:'',
    address:''
  },
  onLoad() {
    app.initPage()
      .then(res => {
        this.getWishPage()
      })
      this.getLocal()
  },
  onShow(){
    this.wxUserGet()
  },
  wxUserGet(){
    app.api.wxUserGet()
      .then(res => {
        this.setData({
          userInfo: res.data
        })
      })
  },
  onShareAppMessage: function () {
    let title = '志愿者招募'
    let path = 'pages/home/index'
    return {
      title: title,
      path: path,
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log(res.errMsg)
        }
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  getLocal() {
    var thiz = this;
    wx.getSetting({
      success (res) {
        console.log(res)
        // 如果没有授权
        if (!res.authSetting['scope.userLocation']) {
          // 则拉起授权窗口
          wx.authorize({ 
            scope: 'scope.userLocation',
            success () {
              wx.getLocation({
                type: 'wgs84',
                success:({ latitude, longitude }) => {
                  const data = {
                    lat: latitude,
                    lon: longitude,
                  }
                  console.log(data)
                  thiz.reverseGeocoder(latitude, longitude)
                }
              })
            }
          })
        }else{
          console.log("---------------------")
          wx.getLocation({
            type: 'wgs84',
            success:({ latitude, longitude }) => {
              console.log(latitude)
              const data = {
                lat: latitude,
                lon: longitude,
              }
              console.log(data)
              thiz.reverseGeocoder(latitude, longitude)
            },
            fail: function (res) {
              console.log(res,'error')
            }
          })
        }
      }
    })
   
  },
  // 解析坐标
  reverseGeocoder(latitude, longitude) {
    var thiz = this;
    QQMap.reverseGeocoder({
      location: {
        latitude,
        longitude
      },
      success:(res) => {
        console.log(res,1)
        thiz.setData({
          address: res.result.address,
          street: res.result.address_component.street,
          street_number: res.result.address_component.street_number,
        })
      }
    })
  },
  // 切换tab
  stateSwitch(e){
    console.log(e.detail.name)
    var state = e.detail.name;
    if(state == 0){
      state = ''
    }
    this.setData({
      state: state
    })
    this.getWishPage()
  },
  // 获取我的报名
  getWishPage() {
    app.api.getApplyByPage({
      searchCount: false,
      current: 1,
      size: 1000,
      state: this.data.state
      // descs: 'create_time'
    })
      .then(res => {
        this.setData({
          wishList: res.data.records
        })
      })
  },
  toSign(e) {
    var paramObj = e.currentTarget.dataset;
    var address = paramObj.address;
    if(this.data.street == ''){
      wx.showToast({
        title: '未授权当前位置，无法完成签到',
        icon: 'none'
      })
      return
    }
    if(address.indexOf(this.data.street) == -1
      || address.indexOf(this.data.street_number) == -1){
        wx.showModal({
          content: '当前位置不在签到区，请到指定地址签到',
          icon: 'none'
        })
        return
    } 
    var params = {
      signFlag: paramObj.flag,
      id: paramObj.id,
      wishId: paramObj.wishid,
    }
    var tips = '签到成功'
    if(params.signFlag == 2){
      tips = '签退成功'
    }
    app.api.applySign(params)
      .then(res => {
        wx.showToast({
          title: tips,
        })
        this.getWishPage()
      })
  },
  refresh(){
    this.setData({
      loadmore: true,
      ['page.current']: 1,
      goodsList: [],
      goodsListNew: [],
    })
  },
  onPullDownRefresh(){
    // 显示顶部刷新图标
    wx.showNavigationBarLoading()
    this.refresh()
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading()
    // 停止下拉动作
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    if (this.data.loadmore) {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      // this.goodsPage()
    }
  },
  jumpPage(e){
    let page = e.currentTarget.dataset.page
    if (page){
      wx.navigateTo({
        url: page
      })
    }
  }
})
